"""
实际可播放视频链接：https://video.pearvideo.com/mp4/short/20250423/cont-1799667-16050326-hd.mp4
数据请求发送的链接：https://www.pearvideo.com/videoStatus.jsp?contId=1799667&mrd=0.20941662254661197
数据请求响应的数据：https://video.pearvideo.com/mp4/short/20250423/1748992127769-16050326-hd.mp4
"""
from email.headerregistry import ContentDispositionHeader

import requests

from utils import os_utils

url = 'https://www.pearvideo.com/video_1799667'
contId = url.split('_')[1]
videoUrl = f'https://www.pearvideo.com/videoStatus.jsp?contId={contId}&mrd=0.20941662254661197'
headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36',
    'Referer': url  # 防盗链：溯源，当前请求的上一级是谁
}
with requests.get(videoUrl, headers=headers) as response:
    # print(response.text)
    data = response.json()
    src_url = data['videoInfo']['videos']['srcUrl']
    system_time = data['systemTime']
    src_url = src_url.replace(system_time, f'cont-{contId}')  # 替换后的最终视频地址
    # print(src_url)
    os_utils.remove('a.mp4')
    with open('a.mp4', 'wb') as f, requests.get(src_url, headers=headers) as response_video:
        f.write(response_video.content)
